/*
 * Project Name: cms_web
 * File Name: MenuAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月10日 上午9:25:18
 */
package com.shishuo.cms.action.manage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.MenuEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.MenuService;

/**
 */
@Controller
@RequestMapping("/admin/menu")
public class MenuAction extends AdminBaseAction
{
	@Autowired
	private MenuService menuService;

	@InitBinder("menuEntity")
	public void initBinder(WebDataBinder binder)
	{
		binder.setFieldDefaultPrefix("menuEntity.");
	}

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addRole(ModelMap modelMap)
	{
		return "system/menu/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewMenu(@ModelAttribute MenuEntity menuEntity) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		if (menuEntity == null || menuEntity.getMenuName() == null || menuEntity.getMenuName().equals(""))
		{
			json.getErrors().put("menuName", "菜单名称不能为空");
		}
		if(validateJsonIsError(json))return json;
		menuService.insert(menuEntity);
		json.setResult(true);
		return json;
	}

	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String RolePage(@RequestParam(value = "p", defaultValue = "1") int pageNum, ModelMap modelMap) throws Exception
	{
		modelMap.put("pageVo", menuService.getAllListPage(pageNum));
		return "system/menu/all";
	}

	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
							@RequestParam(value = "id", defaultValue = "0") int id,
							ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		MenuEntity menuEntity = menuService.getById(id);
		modelMap.put("menuEntity", menuEntity);
		return "system/menu/update";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> updateRole(@ModelAttribute MenuEntity menuEntity) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		if (menuEntity == null || menuEntity.getMenuName() == null || menuEntity.getMenuName().equals(""))
		{
			json.getErrors().put("menuName", "菜单名称不能为空");
		}
		if(validateJsonIsError(json))return json;
		menuService.update(menuEntity);
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delRole(@RequestParam(value = "id", defaultValue = "0") int id) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		if(validateJsonIsError(json))return json;
		menuService.deleteById(id);
		json.setResult(true);
		return json;
	}
	
	
	
	
}
